package com.inspur.incubator.service.impl;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.inspur.incubator.common.MD5Util;
import com.inspur.incubator.common.SecretUtils;
import com.inspur.incubator.common.SerialNumberUtil;
import com.inspur.incubator.common.interceptor.Page;
import com.inspur.incubator.data.MhUser;
import com.inspur.incubator.mapper.MhUserMapper;
import com.inspur.incubator.mapper.RoleDaoMapper;
import com.inspur.incubator.service.MhUserItf;

import net.sf.json.JSONObject;


@Service("MhUserImpl")
public class MhUserImpl implements MhUserItf {
	@Autowired
	private MhUserMapper mhUserMapper;
	@Autowired
	private RoleDaoMapper roleMapper;

	@Override
	public int addUser(MhUser mu) {
		
		if(mu.getUserGtype().equals("1")){
			int mcount = mhUserMapper.mcount()+1;
			String userName= "E_"+String.format("%01d",mcount);
			mu.setUserName(userName);
		}else{
			int mcount = mhUserMapper.mcount()+1;
			String userName= "P_"+String.format("%01d",mcount);
			mu.setUserName(userName);
		}
		mu.setUserPassword(MD5Util.ecodeByMD5(mu.getUserPassword()));
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String createTime=df.format(new Date());
		String userId=SerialNumberUtil.getSerialNumber();
		
		String openId = SerialNumberUtil.getSerialNumber();
		mu.setCreateTime(createTime);
		mu.setUpdateTime(createTime);
		mu.setOpenId(openId);
		mu.setUserId(userId);
		return mhUserMapper.addUser(mu);
	}

	@Override
	public int updateUser(MhUser mu) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String createTime=df.format(new Date());
		mu.setUpdateTime(createTime);
		return mhUserMapper.updateUser(mu);
	}

	@Override
	public MhUser findUser(MhUser mu) {
		return mhUserMapper.findUser(mu);
	}
	@Override
	public List<MhUser> getMusers(MhUser mu,Page page) {
		return mhUserMapper.listPageSelectMUser(mu,page);
	}

	@Override
	public int mcount() {
		// TODO Auto-generated method stub
		return mhUserMapper.mcount();
	}
	
	/**
	 * 
	 * @param response
	 * @param userid
	 * @param username
	 * @param usertel
	 * @param usermail
	 * @param openid
	 */
	public void addCookie(HttpServletResponse response,String userid,String username,String usertel,String usermail,String openid) {
    	Map<String, String> map = new HashMap<String, String>();
        map.put("userid", userid);
        map.put("username", username);
        map.put("usertel", usertel);
        map.put("usermail", usermail);
        map.put("openid", openid);
        map.put("timestamp",System.currentTimeMillis()+"");
        JSONObject jsonObject = JSONObject.fromObject(map);
        System.out.println(jsonObject);
        String miwen ="";
        try {
			miwen = SecretUtils.getcipher(jsonObject.toString());
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        String mingwen="";
        try {
			mingwen =SecretUtils.getorgstr(miwen);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
        
        Cookie cookie;
		try {
			cookie = new Cookie("data",URLEncoder.encode(miwen, "UTF-8"));
			cookie.setDomain(".ibigdatastartup.com");
			//cookie.setPath("/");
			cookie.setMaxAge(365*24*60*60);
			cookie.setHttpOnly(true);
			response.addCookie(cookie);


		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}

	public MhUser findMuser(MhUser mu) {
		// TODO Auto-generated method stub
		return mhUserMapper.findMUser(mu);
	}

}
